<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<div :style="{width: '70px'}"><label>创建时间从:</label></div>
				<el-date-picker v-model="date.createTimeFrom" type="date" @change="dateFilter"></el-date-picker>
				<div :style="{width: '50px','padding-left': '20px'}"><label>至:</label></div>
				<el-date-picker v-model="date.createTimeTo" type="date" @change="dateFilter"></el-date-picker>
			</div>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add">添加</el-button>

			</div>
			<div>
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="关键词"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
					<el-button type="success" icon="el-icon-delete" @click="reset">清空</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
				<el-table-column label="流水号" prop="flowNumber" width="180"/>
				<el-table-column label="订单状态" prop="saleStatus" width="80">
<!--					<template #default="scope">-->
<!--						{{ translateStatus(scope.row.saleStatus) }}-->
<!--					</template>-->
					<template #default="scope">
						<span :class="getStatusClass(scope.row.saleStatus)">
						  {{ translateStatus(scope.row.saleStatus) }}
						</span>
					</template>
				</el-table-column>
				<el-table-column label="买方姓名" prop="customer.name" width="80"/>
				<el-table-column label="重量/斤" prop="amount" width="80"/>
				<el-table-column label="单价/元" prop="price" width="100"/>
				<el-table-column label="总额/元" width="100">
					<template #default="scope">
						{{ getProduct(scope.row) }}
					</template>
				</el-table-column>
				<el-table-column label="实际支付/元" prop="actualTotalPrice" width="100"/>
				<el-table-column label="茶叶种类" prop="productType" width="80">
					<template #default="scope">
						{{ translateProduct(scope.row.productType) }}
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="createDate" width="150"/>
				<el-table-column label="备注" prop="remark" width="200"/>
				<el-table-column label="操作" fixed="right" align="right" width="160">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row)">编辑</el-button>
<!--							<el-button text type="primary" size="small" @click="delete_row(scope.row)">删除</el-button>-->
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
				 @closed="dialog.save=false"></save-dialog>

</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import saveDialog from './save'
import scFileExport from "@/components/scFileExport";
import axios from 'axios';

export default {
	name: 'demo2',
	components: {
		scSelectFilter,
		saveDialog,
		scFileExport
	},
	data() {
		return {
			dialog: {
				save: false
			},
			date: {
				createTimeFrom: {},
				createTimeTo: {},
			},
			list: {
				apiObj: this.$API.factory.demo2.page
			},
			statusMap: {}, // 初始化为空对象
			productMap: {}, // 初始化为空对象
			search: {
				keyword: ""
			}
		}
	},
	created() {
		this.statusMap = this.fetchMap("order_status");
		this.productMap = this.fetchMap("product");
	},
	methods: {
		fetchMap(type) {
			const params = {
				type: type
			};
			const res = {};
			axios.get(`${this.$CONFIG.API_URL}/dict/group`, { params }) // 替换为实际的API路径
				.then(response => {
					for(let a of response.data.data){
						res[a.value] = a.label;
					}
				})
				.catch(error => {
					console.error("获取状态映射时出错:", error);
				});
			return res;
		},
		translateStatus(status) {
			return this.statusMap[status] || '未知';
		},
		translateProduct(pd) {
			return this.productMap[pd] || '未知';
		},
		getProduct(row) {
			if(row.amount == null || row.price == null){
				return null;
			}
			return row.amount * row.price;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//日期搜索
		dateFilter() {
			this.$refs.table.upData({
				createTimeFrom: this.$TOOL.dateFormat(this.date.createTimeFrom, "yyyy-MM-dd"),
				createTimeTo: this.$TOOL.dateFormat(this.date.createTimeTo, "yyyy-MM-dd"),
			})
		},
		// 重置搜索条件
		reset() {
			this.search.keyword = "";
			this.date.createTimeFrom = {};
			this.date.createTimeTo = {};
			// 重载数据，只保留
			this.$refs.table.reload();
		},//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		},
		//窗口编辑
		async table_edit(row) {
			this.dialog.save = true
			this.$TOOL.response(await this.$API.factory.demo2.detail.get(row.id),
				(data) => {
					this.$nextTick(() => {
						this.$refs.saveDialog.open('edit').setData(data)
					})
				})
		},
		//窗口新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//查看
		async table_show(row) {
			this.dialog.save = true
			this.$TOOL.response(await this.$API.factory.demo2.detail.get(row.id),
				(data) => {
					this.$nextTick(() => {
						this.$refs.saveDialog.open('show').setData(data)
					})
				},(res)=>{
					this.$alert(res.message, "错误", {type: 'error'})
				})
		},
		//删除
		async delete_row(row) {
			this.$confirm(`确定删除${row.name}吗？`, '提示', {
				type: 'warning'
			}).then(async ()=>{
				// 删除
				this.$TOOL.response(await this.$API.factory.demo2.delete.delete(row.id),
					() => {
						// 刷新页面
						this.$refs.table.refresh()
						this.$message.success("删除成功")
					},(res)=>{
						this.$alert(res.message, "提示", {type: 'error'})
					})
			}).catch(()=>{
				this.$message.error("处理异常")
			})

		},
		getStatusClass(status) {
			switch (status) {
				case 'OK':
					return 'status-active';
				case 'FREE_FOR_GIVING':
					return 'status-inactive';
				case 'pending':
				default:
					return 'status-pending';
			}
		}
	}
}
</script>

<style scoped>
.status-active { color: green; }
.status-inactive { color: red; }
.status-pending { color: orange; }
</style>
